﻿using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Acr.UserDialogs;
using Cashbook.Models;
using Cashbook.Services;
using Cashbook.Views;
using Xamarin.Forms;

namespace Cashbook.ViewModels
{
	public class BillDetailVM : AbstractViewModel
	{
		private BillsDataAccess _billsService;
		private StatementsDataAccess _statementsService;

		public ObservableRangeCollection<Statement> Statements { get; set; }
		
		private INavigation _navigation;

		public Command LoadBillCommand { get; set; }

		protected IUserDialogs _dalogs { get; }



	

		BillVM _bill;
		public BillVM Bill
		{
			get { return this._bill; }
			set
			{
				if (this._bill == value)
					return;

				this._bill = value;
				this.OnPropertyChanged();
			}
		}

		string _title;
		public string Title
		{
			get { return this._title; }
			set
			{
				if (this._title == value)
					return;

				this._title = value;
				this.OnPropertyChanged();
			}
		}

		
		public BillDetailVM(BillVM bill, INavigation navigation, IUserDialogs dialogs) : base(dialogs)
        {
			_navigation = navigation;
			Bill = bill;
			Title = "帐单详情";

			_billsService = new BillsDataAccess();
            _statementsService = new StatementsDataAccess();

            Statements = new ObservableRangeCollection<Statement>();		 

			LoadBillCommand = new Command(async () => await ExecuteLoadBillCommand());
		
		}



		async Task ExecuteLoadBillCommand()
		{
			

			try
			{
				Statements.Clear();
                var items = await _statementsService.GetStatementsAsync(Bill.Id);
				Statements.ReplaceRange(items);
				
				//Bill = await _billsService.GetBillAsync(BillId);

			}
			catch (Exception ex)
			{
				// Debug.WriteLine(ex);
				await this.Dialogs.AlertAsync("数据加载失败." + ex.Message, "错误", "确定");
			}
		
			
		}


	}
}
